package com.ruoyi.project.system.requirement.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.user.mapper.UserPostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.requirement.mapper.RequirementMapper;
import com.ruoyi.project.system.requirement.domain.Requirement;
import com.ruoyi.project.system.requirement.service.IRequirementService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 需求列Service业务层处理
 * 
 * @author ruoyi
 * @date 2019-11-12
 */
@Service
public class RequirementServiceImpl implements IRequirementService 
{
    @Autowired
    private RequirementMapper requirementMapper;
    @Autowired
    private UserPostMapper userPostMapper;
    /**
     * 查询需求列
     * 
     * @param reqId 需求列ID
     * @return 需求列
     */
    @Override
    public Requirement selectRequirementById(Long reqId)
    {
        return requirementMapper.selectRequirementById(reqId);
    }

    /**
     * 查询需求列列表
     * 
     * @param requirement 需求列
     * @return 需求列
     */
    @Override
    public List<Requirement> selectRequirementList(Requirement requirement)
    {
        //查询实际使用工作量
        List<Requirement> requirements = this.requirementMapper.selectRequirementList(requirement);
        /*if(requirements!=null && requirements.size()>0){
            for (Requirement requirement_temp: requirements){
                requirement_temp.setRealDays(Float.parseFloat(this.requirementMapper.selectWorkRecordBySj(requirement_temp.getReqId())));
            }
        }*/
        return requirements;
    }

    /**
     * 新增需求列
     * 
     * @param requirement 需求列
     * @return 结果
     */
    @Override
    public int insertRequirement(Requirement requirement)
    {
        return requirementMapper.insertRequirement(requirement);
    }

    /**
     * 修改需求列
     * 
     * @param requirement 需求列
     * @return 结果
     */
    @Override
    public int updateRequirement(Requirement requirement)
    {
        return requirementMapper.updateRequirement(requirement);
    }

    /**
     * 删除需求列对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteRequirementByIds(String ids)
    {
        return requirementMapper.deleteRequirementByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除需求列信息
     * 
     * @param reqId 需求列ID
     * @return 结果
     */
    @Override
    public int deleteRequirementById(Long reqId)
    {
        return requirementMapper.deleteRequirementById(reqId);
    }

    /*
    * 保存分配
    * */
    public int saveAssign(String reqIds, Long userids) {

        Long[] requriement = Convert.toLongArray(reqIds);
        List<Requirement> list = new ArrayList<Requirement>();
        for (Long reqId : requriement) {
            Requirement reqm = this.requirementMapper.selectRequirementById(reqId);
            reqm.setReqStatus("02");
            this.requirementMapper.updateRequirement(reqm);
            Requirement requriement1 = new Requirement();
            requriement1.setReqId(reqId);
            requriement1.setDevUser(userids);
            list.add(requriement1);
        }
      return this.requirementMapper.saveAssign(list);
    }

    /**
     * 查询今日添加需求数量
     *
     * @return 结果
     */
    @Override
    public int selectCount()
    {
        return requirementMapper.selectCount();
    }

    /*
    * 详情页面工作总量
    * */
    public String selectWorkRecordBySj(Long reqId){
        return requirementMapper.selectWorkRecordBySj(reqId);
    }
}
